package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.Globalvar;
import com.teach.javafxclient.util.HttpUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.geometry.Rectangle2D;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.stage.FileChooser;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import kong.unirest.json.JSONObject;

import java.io.File;

/**
 * @author Liang Jianlin
 * @date 2024-05-14 01:04
 */
public class EditStudentInfoController {
    @FXML
    public ComboBox<String> provinceCombobox;
    @FXML
    public RadioButton maleButton;
    @FXML
    public RadioButton femaleButton;
    @FXML
    public DatePicker datePicker;
    @FXML
    public TextField telephoneNumber;
    @FXML
    public TextField email;
    @FXML
    public TextField homeAddress;
    @FXML
    public TextArea personalInformation;
    @FXML
    public Button back;
    @FXML
    public Button confirmbutton;
    @FXML
    public Label outputLabel;
    public ToggleGroup sex;
    @FXML
    public VBox avatarUpload;
    @FXML
    public ImageView avatar;
    @FXML
    public Button avatarUploadButton;
    @FXML
    public Label avatarOutputLabel;
    private String studentId;
    JSONObject data = new JSONObject();
    private File selectedFile;

    public void initialize(){
        ObservableList<String> items4= FXCollections.observableArrayList("安徽省","北京市","重庆市","福建省",
                "甘肃省","广东省","广西壮族自治区","贵州省","海南省", "河北省","河南省","黑龙江省", "湖北省", "湖南省",
                "吉林省","江苏省","江西省","辽宁省","内蒙古自治区", "宁夏回族自治区","青海省","山东省","山西省", "陕西省",
                "上海市","四川省","天津市","西藏自治区","新疆维吾尔自治区","云南省","浙江省","台湾省","香港特别行政区","澳门特别行政区");
        provinceCombobox.setItems(items4);
        confirmbutton.setOnAction(e->handleButtonClick());
        Circle clip = new Circle(avatar.getFitWidth() / 2, avatar.getFitHeight() / 2, Math.min(avatar.getFitWidth() , avatar.getFitHeight()) / 2);
        avatar.setClip(clip);
        avatarUpload.setOnMouseClicked(e->{
            FileChooser fileChooser = new FileChooser();
            selectedFile = fileChooser.showOpenDialog(null);
            Image img = new Image(selectedFile.toURI().toString());
            double wid = Math.min(img.getWidth() , img.getHeight());
            Rectangle2D viewport = new Rectangle2D(img.getWidth()/2 - wid/2, img.getHeight()/2 - wid/2, wid, wid);
            avatar.setViewport(viewport);
            avatar.setImage(new Image(selectedFile.toURI().toString()));
        });
        //头像上传
        avatarUploadButton.setOnMouseClicked(e->{
            if(selectedFile == null){
                outputLabel.setText("未选择头像");
                return;
            }
            HttpResponse res = HttpUtil.post(Globalvar.baseUrl+"/api/users/"+ studentId +"/avatar")
                    .field("file",new File(String.valueOf(selectedFile)))
                    .asBytes();
            System.out.println(res.getStatus());
            if (res.getStatus() == 200){
                avatarOutputLabel.setText("修改成功");
            }else{
                avatarOutputLabel.setText("修改失败");
            }
        });
    }
    public void handleButtonClick() {
        boolean comboBoxEmpty = provinceCombobox.getValue() == null;
        boolean radioButtonsSelected = maleButton.isSelected() || femaleButton.isSelected();
        boolean datePickerEmpty = datePicker.getValue() == null;

        if (comboBoxEmpty  && !radioButtonsSelected && datePickerEmpty) {
            outputLabel.setText("生源地未选择！");
        } else if (!radioButtonsSelected) {
            outputLabel.setText("请选择性别！");
        } else if (datePickerEmpty) {
            outputLabel.setText("出生日期未选择！");
        }
        else {
            outputLabel.setText("");
            String gender;
            if(maleButton.isSelected())
                gender="male";
            else
                gender="female";
            String province= provinceCombobox.getValue();
            String birthday= String.valueOf(datePicker.getValue());
            if(email.getText()==null)
                email.setText("john.doe@example.com");
            String email1=email.getText();
            if (telephoneNumber.getText() == null)
                telephoneNumber.setText("1234567890");
            String telephone=telephoneNumber.getText();
            if (homeAddress.getText() == null)
                homeAddress.setText("123 Main St, City");
            String homeAddress1=homeAddress.getText();
            if (personalInformation.getText() == null)
                personalInformation.setText("Some personal information here");
            String personalInfo=personalInformation.getText();
            data.put("province",province);
            data.put("gender", gender);
            data.put("birthday", birthday);
            data.put("email", email1);
            data.put("telephone", telephone);
            data.put("homeAddress", homeAddress1);
            data.put("personalInfo", personalInfo);
            data.put("studentId",studentId);
            HttpResponse<JsonNode> response= HttpUtil.put(Globalvar.baseUrl+"/api/students")
                    .header("Content-Type", "application/json")
                    .body(data)
                    .asJson();
            int statusCode = response.getStatus();
            System.out.println("Status code: " + statusCode);
            if(statusCode == 200){
                outputLabel.setText("修改成功，在下次进入后自动修改学生信息");
            }
        }
    }
    public void setInfo(String provinceS,String genderS,String telephoneS,String emailS,String addressS,String introduceS,String studentId,JSONObject data){
        provinceCombobox.setValue(provinceS);
        telephoneNumber.setText(telephoneS);
        email.setText(emailS);
        homeAddress.setText(addressS);
        personalInformation.setText(introduceS);
        if(genderS.equals("male")){
            maleButton.setSelected(true);
        }else{
            femaleButton.setSelected(true);
        }
        this.data = data;
        this.studentId = studentId;
    }
}
